package com.docker.apps.afterservice.ui;

import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.docker.apps.R;
import com.docker.apps.afterservice.utils.BundleMap;
import com.docker.apps.afterservice.vm.AfterServiceViewModel;
import com.docker.apps.databinding.ProAfterServiceActivityBinding;
import com.docker.common.common.adapter.CommonpagerAdapter;
import com.docker.common.common.command.NitDelegetCommand;
import com.docker.common.common.config.Constant;
import com.docker.common.common.model.CommonListOptions;
import com.docker.common.common.router.AppRouter;
import com.docker.common.common.ui.base.NitCommonActivity;
import com.docker.common.common.ui.base.NitCommonFragment;
import com.docker.common.common.ui.container.NitCommonContainerFragmentV2;
import com.docker.common.common.utils.cache.CacheUtils;
import com.docker.common.common.utils.rxbus.RxBus;
import com.docker.common.common.utils.rxbus.RxEvent;
import com.docker.common.common.vm.NitCommonListVm;
import com.docker.common.common.widget.indector.CommonIndector;
import com.umeng.socialize.UMShareAPI;

import java.util.ArrayList;
import java.util.HashMap;

import io.reactivex.disposables.Disposable;
import timber.log.Timber;

@Route(path = AppRouter.ORDER_AFTER_SERVICE)//售后服务
public class AfterServiceActivity extends NitCommonActivity<AfterServiceViewModel, ProAfterServiceActivityBinding> {

    @Override
    public void initView() {
        mToolbar.setTitle("售后");
        peocessTab();
    }


    @Override
    public void initObserver() {
        mViewModel.mContainerLiveData.observe(this, o -> {

        });
    }

    @Override
    public void initRouter() {
        ARouter.getInstance().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.pro_after_service_activity;
    }

    @Override
    public AfterServiceViewModel getmViewModel() {
        return ViewModelProviders.of(this, factory).get(AfterServiceViewModel.class);
    }

    @Override
    public NitDelegetCommand providerNitDelegetCommand(int flag) {

        NitDelegetCommand nitDelegetCommand = null;

        nitDelegetCommand = new NitDelegetCommand() {
            @Override
            public Class providerOuterVm() {
                return AfterServiceViewModel.class;
            }

            @Override
            public void next(NitCommonListVm commonListVm, NitCommonFragment nitCommonFragment) {
                ((AfterServiceViewModel) commonListVm).state = flag;
                ((AfterServiceViewModel) commonListVm).isAfterService = 1;
            }
        };
        return nitDelegetCommand;
    }

    public ArrayList<Fragment> fragments = new ArrayList<>();

    public void peocessTab() {
        CommonListOptions commonListOptions = new CommonListOptions();
        commonListOptions.refreshState = Constant.KEY_REFRESH_OWNER;
        commonListOptions.isActParent = true;
        commonListOptions.falg = 9;
        commonListOptions.ReqParam.put("memberid", CacheUtils.getUser().uid);
        commonListOptions.ReqParam.put("uuid", CacheUtils.getUser().uuid);
        NitCommonContainerFragmentV2 nitCommonContainerFragmentV2 = NitCommonContainerFragmentV2.newinstance(commonListOptions);
        fragments.add(nitCommonContainerFragmentV2);


//        CommonListOptions commonListOptions1 = new CommonListOptions();
//        commonListOptions1.refreshState = Constant.KEY_REFRESH_OWNER;
//        commonListOptions1.isActParent = true;
//        commonListOptions1.falg = 10;
//        commonListOptions1.ReqParam.put("memberid", CacheUtils.getUser().uid);
//        commonListOptions1.ReqParam.put("uuid", CacheUtils.getUser().uuid);
//        commonListOptions1.ReqParam.put("status", "0,1,4,5");
//        NitCommonContainerFragmentV2 nitCommonContainerFragmentV21 = NitCommonContainerFragmentV2.newinstance(commonListOptions1);

        BundleMap bundleMap = new BundleMap();
        bundleMap.flag[0] = 10;
        bundleMap.flag[1] = 12;
        bundleMap.title[0] = "退/换货";
        bundleMap.title[1] = "发票";

        HashMap<String, String> reqmap = new HashMap<>();
        reqmap.put("memberid", CacheUtils.getUser().uid);
        reqmap.put("uuid", CacheUtils.getUser().uuid);
        reqmap.put("status", "0,1,4,5");
        bundleMap.maps.add(reqmap);

        HashMap<String, String> reqmap1 = new HashMap<>();
        reqmap1.put("memberid", CacheUtils.getUser().uid);
        reqmap1.put("uuid", CacheUtils.getUser().uuid);
        reqmap1.put("status", "0,1");
        bundleMap.maps.add(reqmap1);

        fragments.add(AfterProcessFragment.getInstance(bundleMap));


        BundleMap bundleMap2 = new BundleMap();
        bundleMap2.flag[0] = 11;
        bundleMap2.flag[1] = 13;
        bundleMap2.title[0] = "退/换货";
        bundleMap2.title[1] = "发票";

        HashMap<String, String> reqmap2 = new HashMap<>();
        reqmap2.put("memberid", CacheUtils.getUser().uid);
        reqmap2.put("uuid", CacheUtils.getUser().uuid);
        reqmap2.put("status", "2,3");
        bundleMap2.maps.add(reqmap2);

        HashMap<String, String> reqmap3 = new HashMap<>();
        reqmap3.put("memberid", CacheUtils.getUser().uid);
        reqmap3.put("uuid", CacheUtils.getUser().uuid);
        reqmap3.put("status", "2,3");
        bundleMap2.maps.add(reqmap3);

        fragments.add(AfterProcessFragment.getInstance(bundleMap2));


//        fragments.add(AfterProcessFragment.getInstance(bundleMap));


//        CommonListOptions commonListOptions3 = new CommonListOptions();
//        commonListOptions3.refreshState = Constant.KEY_REFRESH_OWNER;
//        commonListOptions3.isActParent = true;
//        commonListOptions3.falg = 11;
//        commonListOptions3.ReqParam.put("memberid", CacheUtils.getUser().uid);
//        commonListOptions3.ReqParam.put("uuid", CacheUtils.getUser().uuid);
//        commonListOptions3.ReqParam.put("status", "2,3");
//        NitCommonContainerFragmentV2 nitCommonContainerFragmentV3 = NitCommonContainerFragmentV2.newinstance(commonListOptions3);

        String[] titles = new String[]{"退/换货申请", "处理中", "申请记录"};
        // magic
        mBinding.viewPager.setAdapter(new CommonpagerAdapter(this.getSupportFragmentManager(), fragments, titles));
        CommonIndector commonIndector = new CommonIndector();
        commonIndector.initMagicIndicator(titles, mBinding.viewPager, mBinding.magicIndicator, this);

        mBinding.viewPager.setOffscreenPageLimit(3);
        mBinding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                mBinding.serviceNotice.setVisibility(i == 0 ? View.VISIBLE : View.GONE);

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    private Disposable disposable;

    private void initDisposable() {
        disposable = RxBus.getDefault().toObservable(RxEvent.class).subscribe(rxEvent -> {
            if (rxEvent.getT().equals("updateAfterService")) {
                if (mBinding.viewPager.getCurrentItem() == 0) {
                    NitCommonContainerFragmentV2 nitCommonContainerFragmentV2 = (NitCommonContainerFragmentV2) fragments.get(mBinding.viewPager.getCurrentItem());
                    if (nitCommonContainerFragmentV2 != null)
                        nitCommonContainerFragmentV2.mViewModel.mItems.clear();
                    nitCommonContainerFragmentV2.onReFresh(null);
                }
            }
        });
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initDisposable();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (disposable != null) disposable.dispose();
        disposable = null;
    }
}
